package com.cskaoyan.aspect;

import com.cskaoyan.bean.Footprint;
import com.cskaoyan.bean.FootprintExample;
import com.cskaoyan.bean.wx.vo.WxDetailVo;
import com.cskaoyan.mapper.FootprintMapper;
import com.cskaoyan.utils.ShiroGetSubjectMessageUtil;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.session.UnknownSessionException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author tangjian
 * @Date 2021/10/23 19:48
 * @description
 */

@Aspect
@Component
public class WxFootprintAspect {

    @Autowired
    FootprintMapper footprintMapper;

    @Pointcut("execution(* com.cskaoyan.service.impl.GoodsServiceImpl.selectWxDetail(..))")
    public void footprintDetailPointcut(){
    }

    @AfterReturning(value = "footprintDetailPointcut()",returning = "wxDetailVo")
    public void afterFootprintDetail(WxDetailVo wxDetailVo) throws Throwable {
        Integer userID;
        try{
            userID = ShiroGetSubjectMessageUtil.getUserID();
        }catch (NullPointerException e){
            return;
        }catch (UnknownSessionException r){
            return;
        }

        Footprint footprint = new Footprint();
        Integer id = wxDetailVo.getInfo().getId();
        footprint.setGoodsId(wxDetailVo.getInfo().getId());
        footprint.setAddTime(new Date());
        footprint.setUpdateTime(new Date());
        footprint.setDeleted(false);
        footprint.setUserId(userID);

        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);


        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        if (footprints.size() == 0){
            footprintMapper.insert(footprint);
        }else {
            Footprint footprint1 = new Footprint();
            footprint1.setUpdateTime(new Date());
            footprintMapper.updateByExampleSelective(footprint1,footprintExample);
        }
    }


}

